import React, { useState, useEffect, useContext } from 'react'
import Container from '@/appComponents/Container'
import { Table, Spin } from 'antd'
import Filter from './Filter'
import { downloadExcel } from '@/commonjs/util.help.js'
import { getProcessEvaluation, getPlanHistoryList } from '@/api/electricity'
import { LayoutContext } from '&/pages/layouts/context'
import './index.scss'
const EvaluationResults = () => {
  const { activePlan } = useContext(LayoutContext)
  const [loading, setLoading] = useState(false)
  const [timeList, setTimeList] = useState([])
  const [historyId, setHistoryId] = useState()
  const [dataSource, setDataSource] = useState([])
  const [columns, setColumns] = useState([])
  const [paramsData, setParamsData] = useState({
    county_id: -1,
    is_measure: -1
  })
  const [upData, setUpData] = useState([])
  useEffect(() => {
    if (!activePlan.plan_id) return
    getPlanHistoryList({
      plan_id: activePlan.plan_id
    }).then((data) => {
      setTimeList(data)
      setHistoryId(data[data.length - 1].history_id)
    })
  }, [activePlan.plan_id])

  useEffect(() => {
    if (historyId) {
      getHistoryTable()
    } else {
      setDataSource([])
      setColumns([])
    }
  }, [historyId, paramsData])

  const getHistoryTable = () => {
    setLoading(true)
    getProcessEvaluation({
      history_id: historyId,
      type_id: 1,
      plan_id: activePlan.plan_id,
      ...getParams(paramsData)
    }).then((res) => {
      setLoading(false)
      if (res.length) {
        let stringKey = Object.keys(res[0])
        let arrayKey = Object.values(res[0]).filter((i) => Array.isArray(i))[0]
        const columnsData = []
        stringKey.map((item) => {
          if (item != 'data' && item != 'id_list') {
            columnsData.push({
              title: item,
              width: 120,
              dataIndex: item,
              align: 'center'
            })
          }
        })
        arrayKey.map((item) => {
          Object.keys(item).map((i) => {
            if (i != 'id') {
              let obj = {
                title: i,
                width: 200,
                dataIndex: i,
                align: 'center'
              }
              columnsData.push(obj)
            }
          })
        })
        columnsData.unshift({
          title: '序号',
          width: 70,
          dataIndex: 'key',
          align: 'center'
        })
        const dataSource = res.map((item, key) => {
          let obj = {
            key: key + 1
          }
          Object.keys(item).map((i) => {
            if (i != 'data') {
              obj[i] = item[i]
            } else {
              item[i].map((o) => {
                let k = Object.keys(o)
                k.map((t) => {
                  if (t != 'id') {
                    obj[t] = o[t]
                  }
                })
              })
            }
          })
          return obj
        })
        setUpData(dataSource)
        setDataSource(dataSource)
        setColumns(columnsData)
      } else {
        setDataSource([])
        setColumns([])
      }
    })
  }

  const onDownLoadExecl = () => {
    downloadExcel(dataSource, columns, '历史表格')
  }
  const getParams = () => {
    let newParamsData = {}
    for (let key in paramsData) {
      if (paramsData[key] != (-1 || '-1')) {
        newParamsData[key] = paramsData[key]
      }
    }
    return newParamsData
  }
  // 筛选数据方法
  const upDateDataSource = (v) => {
    // 数据评估
    let dataSource = getUpData(v, upData, 'is_measure_voluntarily')
    setDataSource(dataSource)
  }
  // 处理筛选数据
  const getUpData = (v, data, key) => {
    if (v == 1) {
      return data
    }
    let text
    if (v == 2) {
      text = '是'
    } else {
      text = '否'
    }
    let newData = data.filter((i) => {
      return i[key] == text
    })
    return newData
  }

  return (
    <div className="EvaluationResults">
      <Container>
        <Container.ContainerQuery title="查询条件">
          <Filter
            onQuery={getHistoryTable}
            paramsData={paramsData}
            setParamsData={setParamsData}
            timeList={timeList}
            historyId={historyId}
            setHistoryId={setHistoryId}
            onDownLoadExecl={onDownLoadExecl}
            upDateDataSource={upDateDataSource}
          />
        </Container.ContainerQuery>
        <Container.ContainerContent>
          <Spin tip="加载中..." spinning={loading}>
            <Table
              columns={columns}
              dataSource={dataSource}
              bordered
              scroll={{ y: '65vh' }}
              pagination={{
                defaultPageSize: 20,
                position: ['bottomCenter'],
                showQuickJumper: true,
                showTotal: () => `共 ${dataSource.length} 条`
              }}
            />
          </Spin>
        </Container.ContainerContent>
      </Container>
    </div>
  )
}
export default EvaluationResults
